import datetime

from django.db import models 
from django.contrib.auth.models import User

from django.conf import settings

CATEGORY_CHOICES = (
                  (1, 'Market'),
                  (2, 'Cafe'),
                  )

class Store(models.Model):
    name = models.CharField(max_length=100)
    logo = models.TextField(null=True)
    addr = models.TextField(null=True)
    tel = models.TextField(null=True)
    opening = models.TimeField(null=True)
    closing = models.TimeField(null=True)
    x = models.FloatField(default=0.0)
    y = models.FloatField(default=0.0)
    category = models.IntegerField(choices=CATEGORY_CHOICES, default=1)
    registry = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        ordering = ["name"]
    
    def __unicode__(self):
        return self.name

    @models.permalink
    def url(self):
        return ('brispone.views.store.detail', [str(self.id)])

    @models.permalink
    def murl(self):
        return ('brispone.views.store.mdetail', [str(self.id)])
        
    def get_active_events(self):
        return [event for event in self.event_set.all() if event.active]
    
    def get_active_coupons(self):
        coupons = []
        for event in self.event_set.all():
            if event.active:
                coupons.append(event.coupon_set.all())
        return coupons

    def get_coupons(self):
        coupons = []
        for event in self.event_set.all():
            coupons.append(event.coupon_set.all())
        return coupons

class FavoriteStore(models.Manager):
    user = models.ForeignKey(User)
    store = models.ForeignKey(Store)
    registry = models.DateTimeField(auto_now_add=True)

class EventManager(models.Manager):
    def active_events(self):
        return Event.objects.filter(active=True)
    
    def change_active(self):
        events = Event.objects.filter(active=True)
        from datetime import date
        for event in events:
            if event.edate < date.today():
                event.active = False
                event.save()

class Event(models.Model):
    store = models.ForeignKey(Store)
    title = models.CharField(max_length=100)
    sdate = models.DateField(null=True)
    edate = models.DateField(null=True)
    description = models.TextField()
    active = models.BooleanField(default=True)
    registry = models.DateTimeField(auto_now_add=True)
    objects = EventManager()

    class Meta:
        ordering = ["-edate"]
        
    def __unicode__(self):
        return '%s %s' % (self.store.name, self.title)
    
    def save(self, *args, **kwargs):
        today = datetime.date.today()
        if self.sdate <= today and today < self.edate:
            self.active = True
        super(Event, self).save(*args, **kwargs)
        
    @models.permalink
    def url(self):
        return ('brispone.views.event.detail', [str(self.id)])

    @models.permalink
    def murl(self):
        return ('brispone.views.event.mdetail', [str(self.id)])

    def get_active_coupons(self):
        return None if not self.active else self.coupon_set.all() 

class Coupon(models.Model):
    name = models.CharField(max_length=150, null=True)
    event = models.ForeignKey(Event)
    start_count = models.IntegerField(settings.DEFAULT_COUPON)
    current_count = models.IntegerField(settings.DEFAULT_COUPON) 
    textmsg = models.CharField(max_length=150, null=True)
    registry = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        ordering = ["name"]
    
    def __unicode__(self):
        return self.name
    
    @models.permalink
    def url(self):
        return ('brispone.views.coupon.detail', [str(self.id)])

    @models.permalink
    def murl(self):
        return ('brispone.views.coupon.mdetail', [str(self.id)])

    def available(self):
        return True if self.current_count > 0 else False

    def count_down(self):
        if self.current_count > 1:
            self.current_count = self.current_count - 1
            self.save()

    def current_percent(self):
        return self.start_count / self.current_count * 100
    
class FavoriteCoupon(models.Model):
    user = models.ForeignKey(User)
    coupon = models.ForeignKey(Coupon)
    registry = models.DateTimeField(auto_now_add=True)
